package com.zhida.jiyangcenter.modle

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.loadState.LoadService
import chooongg.base.loadState.LoadUtils
import chooongg.base.loadState.callback.EmptyCallback
import chooongg.base.loadState.callback.ErrorCallback
import chooongg.base.loadState.callback.NetworkCallback
import chooongg.base.utils.visible
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.http.APIException
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.DEFAULT_ROW
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.DeviceListEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.list.*

@TitleBar(false)
@ContentLayout(R.layout.list)
class EquipmentListActivity : BaseActivity() {
    var page = 1
    val adapter = Adapter()
    private lateinit var loadService: LoadService<*>
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.visible()
        title_bar.title("管理设备").navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        loadService = LoadUtils.getDefault().register(refresh_layout) {
            page = 1
            getData()
        }
        recycler_view.layoutManager = LinearLayoutManager(context)
        recycler_view.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(context).sizeResId(R.dimen.d32)
                .colorResId(R.color.colorBack).build()
        )
        recycler_view.adapter = adapter
        refresh_layout.setOnRefreshListener {
            page = 1
            getData()
        }
        adapter.setPreLoadNumber(3)
        adapter.setOnLoadMoreListener({
            ++page
            getData()
        }, recycler_view)
        getData()
    }

    private fun getData() {
        lifecycleScope.request({ APIAccount.service().deviceList(page) },
            object : BaseAPI<List<DeviceListEntity>>() {
                override fun onSuccess(t: List<DeviceListEntity>?, state: Int, msg: String) {
                    if (t != null && !t.isNullOrEmpty()) {
                        if (page <= 1) {
                            adapter.setNewData(t)
                            loadService.showSuccess()
                            refresh_layout.finishRefresh()
                            if (t.size < DEFAULT_ROW) {
                                adapter.loadMoreEnd(true)
                            }
                        } else {
                            adapter.addData(t)
                            if (t.size < DEFAULT_ROW) {
                                adapter.loadMoreEnd(true)
                            } else adapter.loadMoreComplete()
                        }
                    } else onError(APIException(APIException.Type.EMPTY))
                }

                override fun onError(e: APIException) {
                    if (page <= 1) when (e.type) {
                        APIException.Type.EMPTY -> loadService.showCallback(
                            EmptyCallback::class,
                            "暂无设备"
                        )
                        APIException.Type.ERROR_NETWORK -> loadService.showCallback(NetworkCallback::class)
                        else -> loadService.showCallback(ErrorCallback::class, e.eMessage)
                    } else when (e.type) {
                        APIException.Type.EMPTY -> {
                            adapter.loadMoreFail()
                        }
                        else -> adapter.loadMoreFail()
                    }
                }

            })
    }

    class Adapter : BaseQuickAdapter<DeviceListEntity, BaseViewHolder>(R.layout.item_equipment) {
        override fun convert(helper: BaseViewHolder, item: DeviceListEntity?) {
            glideImage(helper.getView(R.id.niv_type), item?.picture ?: "")
            helper.setText(R.id.tv_name, item?.name)
        }

    }
}